package com.yunji.base.engine.validate;

import com.yunji.base.dto.SiteDTO;
import com.yunji.base.service.SiteService;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.exception.ServiceException;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author : peter-zhu
 * @date : 2025/1/8 09:33
 * @description : TODO
 **/
@Slf4j
@Component
public class SiteValidate {

    private static SiteService siteService;

    public static void validatePhone(String siteTel) {
        if (!SysUserValidate.existUserName(siteTel))
            throw new ServiceException(ErrorEnum.BUSINESS_121001.getText());
    }

    @Autowired
    protected void setSiteService(SiteService siteService) {
        SiteValidate.siteService = siteService;
    }

    /**
     * @param dto 网点信息
     * @Title checkSite
     * @Desc 检查网点信息
     * @Date 2024-05-30 14:10:33.489
     */
    public static void validateSiteParam(SiteDTO dto) {
        String name = dto.getSiteName();
        Assert.isTrue(BeanUtils.isNotNull(name), ErrorEnum.CHECKED_611001.getText());
        Assert.isTrue((name.length() <= 15), ErrorEnum.CHECKED_611002.getText());
        Integer type = dto.getSiteType();
        Assert.isTrue(BeanUtils.isNotNull(type), ErrorEnum.CHECKED_611003.getText());
        SiteDO site = new SiteDO();
        site.setId(dto.getId());
        site.setSiteName(name);
        Assert.isTrue(!siteService.checkRepeatName(site), ErrorEnum.BUSINESS_612001.getText());
        String region = dto.getRegion();
        // 校验网点地址
        if (BeanUtils.isNull(region)) {
            return;
        }
        Assert.isTrue((region.length() <= 25), ErrorEnum.CHECKED_611004.getText());
    }

}
